<?php

/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2023-05-17 15:50:14
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2023-06-07 13:10:42
 */

namespace Common\Services;

use Common\BaseService;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Swoole\WebSocket\Frame;

class CurlClient extends BaseService
{
    /*
     * url内容请看 tests/index.php
     */
    private static string $baseUrl = '';

    private static $client;

    // string	业务字段
    public static string $data = '';


    private static array $header = [
        'Content-Type' => 'text/json;charset=utf-8',
        'Content-Version' => '1.0',
    ];

    public function __construct(?string $baseUrl = null)
    {
        self::$client = new Client([
            'url' => $baseUrl
        ]);
    }


    public static function buildHeader($headers): array
    {
        return array_merge(self::$header, $headers);
    }

    /**
     * GET请求
     * @param string $url
     * @param array $params
     * @param array $header
     * @param array $Cookies
     * @return array
     * @throws GuzzleException
     * @date 2023-05-17
     * @example
     * @author Wang Chunsheng
     * @since
     */
    public function Get(string $url, array $params, $header = [], $Cookies = [])
    {
        $client = self::$client;
        $client->setPath($url);
        $client->setHeaders(self::buildHeader($header));
        $client->addCookies($Cookies);
        $client->setQuery(['arg2' => 3, 'q' => 2]);
        $response = $client->get(self::buildHeader($header));
        return self::analysisRes($response);
    }


    public function Delete(string $url, $header = [], $Cookies = [])
    {
        $client = self::$client;
        $client->setPath($url);

        $client->setHeaders(self::buildHeader($header));
        $client->addCookies($Cookies);
        $response = $client->delete();

        return self::analysisRes($response);
    }


    public function Put(string $url, $header = [], $Cookies = [])
    {
        $client = self::$client;
        $client->setPath($url);

        $client->setHeaders(self::buildHeader($header));
        $client->addCookies($Cookies);

        $response = $client->put('testPut');
        return self::analysisRes($response);
    }

    public function Post(string $url, array $params, $header = [], $Cookies = [])
    {
        $client = self::$client;
//        $client->setPath($url);
        $client->setHeaders(self::buildHeader($header));
        $client->addCookies($Cookies);
        $response = $client->post($url,$params);
        echo '请求没解析';
        print_r($response);
        return self::analysisRes($response);
    }


    public function Options(string $url, array $params, $header = [], $Cookies = [])
    {
        $client = self::$client;
        $client->setPath($url);

        $client->setHeaders(self::buildHeader($header));
        $client->addCookies($Cookies);

        $response = $client->options($params);
        return self::analysisRes($response);
    }

    public function Patch(string $url, $params, $header = [], $Cookies = [])
    {
        $client = self::$client;
        $client->setPath($url);

        $client->setHeaders(self::buildHeader($header));
        $client->addCookies($Cookies);

        $response = $client->patch($params);
        return self::analysisRes($response);
    }


    public function PostXml(string $url, $params, $header = [], $Cookies = [])
    {
        $client = self::$client;
        $client->setPath($url);

        $client->setHeaders(self::buildHeader($header));
        $client->addCookies($Cookies);

        $response = $client->postXml($params);
        return self::analysisRes($response);
    }

    public function PostJson(string $url, array $params, $header = [], $Cookies = [])
    {
        $client = self::$client;
        $client->setPath($url);
        $client->setHeaders(self::buildHeader($header));
        $client->addCookies($Cookies);
        $client->setConnectTimeout(15);
        $response = $client->postJson(json_encode($params));
        return self::analysisRes($response);
    }


    public function PostString(string $url, $params, $header = [], $Cookies = [])
    {
        $client = self::$client;
        $client->setPath($url);
        $client->setHeaders(self::buildHeader($header));
        $client->addCookies($Cookies);
        $response = $client->post($params);
        return self::analysisRes($response);
    }


    /**
     * 测试websocket，需要自己实现websocket服务器
     * testWebsocket
     * @throws \EasySwoole\HttpClient\Exception\InvalidUrl
     * @author tioncico
     * Time: 下午12:26
     */
    public function Websocket($ip, $port, $headers = [])
    {
        $client = new HttpClient('$ip:$port');
        $client->setHeaders($headers);
        $upgradeResult = $client->upgrade(true);
        $recvFrame = $client->recv();

        $frame = new Frame();
        $frame->data = json_encode(['action' => 'hello', 'content' => ['a' => 1]]);
        $pushResult = $client->push($frame);


        $recvFrame = $client->recv();
        $recvFrame->data;
    }

    public function BasicAuth()
    {
        $httpClient = new HttpClient('127.0.0.1:9510');
        $httpClient->setBasicAuth('admin', '111111');
        $response = $httpClient->post();
        return self::analysisRes($response);
    }

    public function FollowLocation()
    {
        $httpClient = new HttpClient('https://www.gaobinzhan.com/blog');
        $httpClient->enableFollowLocation(0);
        $response = $httpClient->get();
        $status = $response->getStatusCode();


        $httpClient = new HttpClient('https://www.gaobinzhan.com/blog');
        $response = $httpClient->get();
        $status = $response->getStatusCode();

        $httpClient->enableFollowLocation(0);
        $response = $httpClient->get();
        $status = $response->getStatusCode();
    }

    public function SetPath()
    {
        $httpClient = new HttpClient('https://www.easyswoole.com/demo.html');
        $res = $httpClient->get();
        $res = $res->getBody();
        $httpClient->setPath('/Preface/intro.html');
        $res = $httpClient->get();
        $res = $res->getBody();
    }

    public function SetMethod()
    {
        $httpClient = new HttpClient('https://www.easyswoole.com/Cn/demo.html');
        $httpClient->setMethod('get');
        $httpClient->getClient()->requestMethod;
        $httpClient->setMethod('post');
        $httpClient->setMethod('put');
        $httpClient->setMethod('delete');
    }

    public function HttpProxy()
    {
        $httpClient = new HttpClient('https://www.google.com');
        $response = $httpClient->get();

        $httpClient->setProxyHttp('127.0.0.1', 1087);
        $response = $httpClient->get();
    }

    public function SocketProxy()
    {
        $httpClient = new HttpClient('https://www.google.com');
        $response = $httpClient->get();
        $httpClient->setProxySocks5('127.0.0.1', 1086);
        $response = $httpClient->get();
    }

    public static function analysisRes($res)
    {
        $data = json_decode($res->getBody(), true);
        $status = $res->getStatusCode();

        return [
            'data' => $data,
            'status' => $status,
        ];
    }
}
